package com.chagee.channel.server.config;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ocr配置
 *
 * @author: chang.jiawei
 * @date: 2024/01/15
 */
@Data
@Configuration
@Component
@RefreshScope
@ConfigurationProperties(prefix = "ocr")
public class OcrConfig {

    public static final String CERTIFICATION_TYPE = "access_key";

    private AccessKey accessKey;

    private String endpoint;

    /**
     * 是否检查bucket
     */
    private Boolean checkBucket;
    /**
     * 需要检查的bucket域名
     */
    private List<String> checkBucketDomains;

    @Data
    public static class AccessKey {

        private String id;

        private String secret;
    }

    @Bean
    @AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
    @Lazy
    public Client client() throws Exception {
        Config config = new Config();
        config.setEndpoint(this.endpoint)
            .setType(CERTIFICATION_TYPE)
            .setAccessKeyId(this.accessKey.id)
            .setAccessKeySecret(this.accessKey.secret);
        return new Client(config);
    }

}
